package com.anche.cywebnew.Controller.configManager;

import com.anche.cywebnew.Pojo.configManager.SysServerCluster;
import com.anche.cywebnew.Service.configManager.ServerClusterService;
import com.anche.cywebnew.Utils.annotation.OptLog;
import com.anche.cywebnew.Utils.enums.OptType;
import com.anche.cywebnew.Utils.result.Result;
import com.anche.cywebnew.Utils.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/config/cluster")
public class ServerClusterController {

    @Autowired
    private ServerClusterService serverClusterService;

    @RequestMapping("/manager")
    public String toClusterPage() {
        return "configManager/cluster";
    }

    @GetMapping("/serverList")
    @ResponseBody
    public Object getServerList() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<SysServerCluster> sysServerClusters = serverClusterService.getServerList();
            map.put("code", "0");
            map.put("count", sysServerClusters.size());
            map.put("msg", "查询成功");
            map.put("data", sysServerClusters);
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            map.put("code", "1");
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @PostMapping("/clusterInfo")
    @ResponseBody
    @OptLog(title = "添加信息", type = OptType.ADD)
    public Object addServerCluster(SysServerCluster cluster) {
        try {
            serverClusterService.addServerCluster(cluster);
            return ResultUtil.success("新增成功");
        } catch (DuplicateKeyException dupli) {
            return ResultUtil.error(0, "IP已存在！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @PutMapping("/clusterInfo")
    @ResponseBody
    @OptLog(title = "修改信息", type = OptType.UPDATE)
    public Object setServerCluster(SysServerCluster cluster) {
        try {
            serverClusterService.setServerCluster(cluster);
            return ResultUtil.success("修改成功");
        } catch (DuplicateKeyException dupli) {
            return ResultUtil.error(0, "IP已存在！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }

    @DeleteMapping("/clusterInfo")
    @ResponseBody
    @OptLog(title = "删除信息", type = OptType.DEL)
    public Object delServerCluster(@RequestParam("id") String id) {
        try {
            serverClusterService.delServerCluster(id);
            return ResultUtil.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(0, e.getMessage());
        }
    }
}
